﻿#ifndef FWIDGETS_PRIVATE_H
#define FWIDGETS_PRIVATE_H


#include <QResizeEvent>
#include <QPainter>
#include "FTitleBar.h"

class FWidgetsObject
{
public:
    inline static QString backgroundImagePath = "";
};


#define FWIDGET_COMMON_FILEIDS(_BaseClass)                                         \
public:                                                                            \
                                                                                   \
    FTitleBar* titleBar() const                                                    \
    {                                                                              \
        return m_tb;                                                               \
    }                                                                              \
                                                                                   \
    void setWindowTitle(const QString& title)                                      \
    {                                                                              \
        _BaseClass::setWindowTitle(title);                                         \
        m_tb->setTitle(title);                                                     \
    }                                                                              \
                                                                                   \
    void setWindowIcon(const QIcon& icon)                                          \
    {                                                                              \
        _BaseClass::setWindowIcon(icon);                                           \
        m_tb->setIcon(icon);                                                       \
    }                                                                              \
                                                                                   \
    void showMaximized()                                                           \
    {                                                                              \
        _BaseClass::showMaximized();                                               \
        m_tb->setMaxIcon(false);                                                   \
    }                                                                              \
                                                                                   \
    bool setBackgroundImage(const QString &path)                                   \
    {                                                                              \
        if (!m_bg.load(path)) {                                                    \
            return false;                                                          \
        }                                                                          \
                                                                                   \
        update();                                                                  \
        return true;                                                               \
    }                                                                              \
                                                                                   \
    bool setGlobalBackgroundImage(const QString &path)                             \
    {                                                                              \
        if (!setBackgroundImage(path)) return false;                               \
        FWidgetsObject::backgroundImagePath = path;                                \
        return true;                                                               \
    }                                                                              \
                                                                                   \
private slots:                                                                     \
    void paintEvent(QPaintEvent *event) override                                   \
    {                                                                              \
        _BaseClass::paintEvent(event);                                             \
        if (!m_bg.isNull()) {                                                      \
            QPainter painter(this);                                                \
            QPixmap scaled = m_bg.scaled(                                          \
                size(), Qt::KeepAspectRatioByExpanding,                            \
                Qt::SmoothTransformation);                                         \
                                                                                   \
            int x = (width()  - scaled.width())  / 2;                              \
            int y = (height() - scaled.height()) / 2;                              \
            painter.drawPixmap(x, y, scaled);                                      \
            painter.fillRect(rect(), QColor(250, 250, 250, 200));                  \
        }                                                                          \
    }                                                                              \
                                                                                   \
    void resizeEvent(QResizeEvent* e) override                                     \
    {                                                                              \
        m_tb->resize(e->size().width(), m_tb->height());                           \
        _BaseClass::resizeEvent(e);                                                \
    }                                                                              \
                                                                                   \
    void changeEvent(QEvent *event) override                                       \
    {                                                                              \
        if (event->type() == QEvent::WindowStateChange) {                          \
            m_tb->onParentMaximized(                                               \
                windowState().testFlag(Qt::WindowMaximized)                        \
            );                                                                     \
        }                                                                          \
                                                                                   \
        _BaseClass::changeEvent(event);                                            \
    }                                                                              \
                                                                                   \
    void init()                                                                    \
    {                                                                              \
        m_tb = new FTitleBar(this);                                                \
        setContentsMargins(0, m_tb->height(), 0, 0);                               \
        setAttribute(Qt::WA_StyledBackground);                                     \
        setBackgroundImage(FWidgetsObject::backgroundImagePath);                   \
    }                                                                              \
                                                                                   \
private:                                                                           \
    QPixmap m_bg;                                                                  \
    FTitleBar* m_tb;                                                               /* end macro */


#endif // FWIDGETS_PRIVATE_H
